float4x4 View;
float4x4 Projection;
float3 LightDirection = float3(1, -1, 0);
float TextureTiling = 1;

texture GroundTexture;
sampler GroundTextureSampler = sampler_state
{
	Texture = <GroundTexture>;
	AddressU = clamp;
	AddressV = clamp;
	MinFilter = Anisotropic;
	MagFilter = Anisotropic;
	MipFilter = Linear;
};

texture GrassTexture;
sampler GrassTextureSampler = sampler_state
{
	Texture = <GrassTexture>;
	AddressU = clamp;
	AddressV = clamp;
	MinFilter = Anisotropic;
	MagFilter = Anisotropic;
	MipFilter = Linear;
};

texture SnowTexture;
sampler SnowTextureSampler = sampler_state
{
	Texture = <SnowTexture>;
	AddressU = clamp;
	AddressV = clamp;
	MinFilter = Anisotropic;
	MagFilter = Anisotropic;
	MipFilter = Linear;
};

texture RoadTexture;
sampler RoadTextureSampler = sampler_state
{
	Texture = <RoadTexture>;
	AddressU = Wrap;
	AddressV = Wrap;	
	MinFilter = Anisotropic;
	MagFilter = Anisotropic;
};

float DetailTextureTiling = 6;
float DetailDistance = 100;

texture DetailTexture;
sampler DetailTextureSampler = sampler_state
{
	Texture = <DetailTexture>;
	AddressU = Wrap;
	AddressV = Wrap;	
	MinFilter = Anisotropic;
	MagFilter = Anisotropic;
};

struct VertexShaderInput
{
	float4 Position : POSITION;	
	float3 Normal : NORMAL;
	float2 UV : TEXCOORD;
};

struct VertexShaderOutput
{
	float4 Position : POSITION0;
	float2 UV : TEXCOORD0;
	float3 Normal : TEXCOORD1;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
	VertexShaderOutput output;

	output.Position = mul(input.Position, mul(View, Projection));
	output.Normal = input.Normal;
	output.UV = input.UV;

	return output;
}

struct PixleShaderInput
{
	float2 UV : TEXCOORD0;
	float3 Normal : TEXCOORD1;	
	float Depth : TEXCOORD2;
};

float4 PixelShaderFunction(PixleShaderInput input) : COLOR0
{
	float light = dot(normalize(input.Normal), normalize(LightDirection));

	light = clamp(light + 0.4f, 0, 1);

	float3 groundSample = tex2D(GroundTextureSampler, input.UV * TextureTiling);

	float3 color = float3(0, 0, 0);
	color += tex2D(GrassTextureSampler, input.UV * TextureTiling) * groundSample.r;
	color += tex2D(SnowTextureSampler, input.UV * TextureTiling) * groundSample.b;
	color += tex2D(RoadTextureSampler, input.UV * TextureTiling) * groundSample.g;

	float3 detail = tex2D(DetailTextureSampler, input.UV * DetailTextureTiling);
	float detailAmt = input.Depth / DetailDistance;
	detail = lerp(detail, 1, clamp(detailAmt, 0, 1));

	return float4(detail * color * light, 1);
}

technique Technique1
{
	pass Pass1
	{
		VertexShader = compile vs_2_0 VertexShaderFunction();
		PixelShader = compile ps_2_0 PixelShaderFunction();
	}
}